﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq;
using Hxsoft.AppV1;

namespace Hxsoft.AppV1.Module
{
	/// <summary>
	/// UsersInRoles 类。
	/// </summary>
	internal static partial class UsersInRoles
	{
		#region Exists
		/// <summary>
		/// 验证指定的用户角色是否已经存在。
		/// </summary>
		/// <param name="o">用户角色集合。</param>
		/// <param name="username">验证的用户名。</param>
		/// <param name="siteId">角色所属的站点。</param>
		/// <param name="roleName">验证的角色名。</param>
		/// <returns>布尔值表示指定的用户角色是否已经存在。</returns>
		public static bool Exists(this System.Data.Linq.Table<ViewUsersInRole> o, string username, int? siteId, string roleName)
		{
			if(siteId.HasValue)
				return o.Any(p => p.UserName == username && p.SiteId == siteId.Value && p.RoleName == roleName);
			return o.Any(p => p.UserName == username && p.SiteId == null && p.RoleName == roleName);
		}

		/// <summary>
		/// 验证指定的用户角色是否已经存在。
		/// </summary>
		/// <param name="username">验证的用户名。</param>
		/// <param name="siteId">角色所属的站点。</param>
		/// <param name="roleName">验证的角色名。</param>
		/// <returns>布尔值表示指定的用户角色是否已经存在。</returns>
		public static bool Exists(string username, int? siteId, string roleName)
		{
			GenericModuleDataContext dc = new GenericModuleDataContext();
			dc.ObjectTrackingEnabled = false;
			return dc.ViewUsersInRoles.Exists(username, siteId, roleName);
		}




		/// <summary>
		/// 验证指定的用户角色是否已经存在。
		/// </summary>
		/// <param name="o">用户角色集合。</param>
		/// <param name="userId">验证的用户标识。</param>
		/// <param name="roleId">验证的角色标识。</param>
		/// <returns>布尔值表示指定的用户角色是否已经存在。</returns>
		public static bool Exists(this System.Data.Linq.Table<UsersInRole> o, Guid userId, Guid roleId)
		{
			return o.Any(p => p.UserId == userId && p.RoleId == roleId);
		}

		/// <summary>
		/// 验证指定的用户角色是否已经存在。
		/// </summary>
		/// <param name="userId">验证的用户标识。</param>
		/// <param name="roleId">验证的角色标识。</param>
		/// <returns>布尔值表示指定的用户角色是否已经存在。</returns>
		public static bool Exists(Guid userId, Guid roleId)
		{
			GenericModuleDataContext dc = new GenericModuleDataContext();
			dc.ObjectTrackingEnabled = false;
			return dc.UsersInRoles.Exists(userId, roleId);
		}
		#endregion



		#region GetItem
		/// <summary>
		/// 获取指定的用户角色。
		/// </summary>
		/// <param name="o">用户角色集合。</param>
		/// <param name="userId">用户标识。</param>
		/// <param name="roleId">角色标识。</param>
		/// <returns>获取的用户角色。</returns>
		public static UsersInRole GetItem(this System.Data.Linq.Table<UsersInRole> o, Guid userId, Guid roleId)
		{
			return o.SingleOrDefault(p => p.UserId == userId && p.RoleId == roleId);
		}

		/// <summary>
		/// 获取指定的用户角色。
		/// </summary>
		/// <param name="userId">用户标识。</param>
		/// <param name="roleId">角色标识。</param>
		/// <returns>获取的用户角色。</returns>
		public static UsersInRole GetItem(Guid userId, Guid roleId)
		{
			GenericModuleDataContext dc = new GenericModuleDataContext();
			dc.ObjectTrackingEnabled = false;
			return dc.UsersInRoles.GetItem(userId, roleId);
		}
		#endregion



		public static Guid[] GetRolesForUser(this System.Data.Linq.Table<ViewUsersInRole> o, string username)
		{
			return (from p in o where p.UserName == username select p.RoleId).ToArray();
		}

		public static Guid[] GetRolesForUser(string username)
		{
			GenericModuleDataContext dc = new GenericModuleDataContext();
			dc.ObjectTrackingEnabled = false;
			return dc.ViewUsersInRoles.GetRolesForUser(username);
		}



		#region GetUsersInRole
		public static string[] GetUsersInRole(this System.Data.Linq.Table<ViewUsersInRole> o, int? siteId, string roleName)
		{
			IQueryable<string> q;
			if(siteId.HasValue)
				q = from p in o where p.SiteId == siteId.Value && p.RoleName == roleName select p.UserName;
			else
				q = from p in o where p.SiteId == null && p.RoleName == roleName select p.UserName;
			return q.ToArray();
		}

		public static string[] GetUsersInRole(int? siteId, string roleName)
		{
			GenericModuleDataContext dc = new GenericModuleDataContext();
			dc.ObjectTrackingEnabled = false;
			return dc.ViewUsersInRoles.GetUsersInRole(siteId, roleName);
		}



		public static string[] GetUsersInRole(this System.Data.Linq.Table<ViewUsersInRole> o, Guid roleId)
		{
			var q = from p in o where p.RoleId == roleId select p.UserName; ;
			return q.ToArray();
		}

		public static string[] GetUsersInRole(Guid roleId)
		{
			GenericModuleDataContext dc = new GenericModuleDataContext();
			dc.ObjectTrackingEnabled = false;
			return dc.ViewUsersInRoles.GetUsersInRole(roleId);
		}
		#endregion
	}




	/// <summary>
	/// UsersInRoles 类操作部分。
	/// </summary>
	internal static partial class UsersInRoles
	{
		internal static void AddUserToRole(this System.Data.Linq.Table<UsersInRole> o, string userName, Guid roleId)
		{
			Guid? userId = Users.GetUserId(userName);
			if (userId == null) return;

			if (o.Exists(userId.Value, roleId)) return;
			o.InsertOnSubmit(new UsersInRole() { UserId = userId.Value, RoleId = roleId });
		}

		internal static void AddUserToRole(string userName, Guid roleId)
		{
			GenericModuleDataContext dc = new GenericModuleDataContext();
			dc.UsersInRoles.AddUserToRole(userName, roleId);
			dc.SubmitChanges();
		}



		internal static void RemoveUsersFromRole(this System.Data.Linq.Table<UsersInRole> o, string[] userNames, Guid roleId)
		{
			if (userNames == null || userNames.Length == 0) return;

			foreach (string userName in userNames)
			{
				Guid? userId = Users.GetUserId(userName);
				if (userId == null) continue;

				UsersInRole usersInRole = o.GetItem(userId.Value, roleId);
				if (usersInRole != null) o.DeleteOnSubmit(usersInRole);
			}
		}

		internal static void RemoveUsersFromRole(string[] userNames, Guid roleId)
		{
			GenericModuleDataContext dc = new GenericModuleDataContext();
			dc.UsersInRoles.RemoveUsersFromRole(userNames, roleId);
			dc.SubmitChanges();
		}
	}
}